package com.peoit.honeycomb.controller;

import java.util.List;

import com.peoit.honeycomb.dto.*;
import com.peoit.honeycomb.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.peoit.honeycomb.application.InformationApplication;
import com.peoit.honeycomb.domain.Information;

@Controller
@RequestMapping("/admin/information")
public class InformationController extends HoneycombBaseController {

	@Autowired
	InformationApplication informationApplication;

	/**
	 * 查询资讯列表
	 *
	 * @return
	 */
	@RequestMapping("/list/{offset}")
	public ModelAndView list(InformationQueryDto dto,@PathVariable int offset) {
		dto.setOffset(offset);
		PageCommonResultDto pageCommonResultDto = new PageCommonResultDto();
		List<Information> informations = informationApplication.findInformations(dto);
		long count = informationApplication.findByWhereCount(dto);
		pageCommonResultDto.setResult(informations);
		pageCommonResultDto.setPageSize(dto.getPageSize());
		pageCommonResultDto.setCount(count);
		pageCommonResultDto.setOffset(offset);
		return view("/jsp/Information/informationList", pageCommonResultDto);
	}

	/**
	 * 到添加资讯页面
	 * 
	 * @return
	 */
	@RequestMapping("/toadd")
	public ModelAndView toAdd(InformationDto dto) {

		return view("/jsp/Information/addInformation", dto);
	}

	/**
	 * 添加资讯信息
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping("/addInformation")
	public ModelAndView addInformation(InformationDto dto,@RequestParam(required = false, value = "infopic") MultipartFile infopic) {
		if (infopic.getOriginalFilename() != null && !infopic.getOriginalFilename().equals("")) {
			UploadUtil uploadUtil = new UploadUtil();
			String path = uploadUtil.getUploadPicPath(infopic);
			dto.setPicturePath(path);
		}
		informationApplication.addInformation(dto);
		return view("redirect:/admin/information/list/0");
	}

	/**
	 * 到资讯修改页面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/toupdate")
	public ModelAndView toUpdate(String id) {
		return view("/jsp/Information/addInformation", informationApplication.informationDetail(id));
	}

	/**
	 * 获取资讯详细信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/informationDetail")
	public ModelAndView informationDetail(String id) {
		return view("/jsp/Information/InformationDetail", informationApplication.informationDetail(id));
	}

	/**
	 * 获取资讯详细信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delInformation")
	public @ResponseBody CommonResultJsonDto delInformation(String id) {
		CommonResultJsonDto result = CommonResultJsonDto.success("成功！！");
		try {
			informationApplication.deleteInformation(id);
		} catch (Exception e) {
			e.printStackTrace();
			result = CommonResultJsonDto.fail(e.getMessage());
		}
		return result;
	}
}
